package cn.smile.config;

import cn.smile.filter.JwtFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;

/**
 * JWT 配置类
 *
 * @Author maomao
 */
@Configuration
public class JwtConfig {


    /**
     * 使用集合配置URL比较方便
     */
    private List<String> URL_PATTERNS = new ArrayList<>(20);

    /**
     * 初始化URL
     */
    {
        URL_PATTERNS.add("/secure/*");
        URL_PATTERNS.add("/privacy/*");
    }


    /**
     * 注册JWT过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean jwtFilter() {
        String[] uri=new String[URL_PATTERNS.size()];
        URL_PATTERNS.toArray(uri);
        //注册过滤器的类
        final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        //注册Jwt过滤器
        registrationBean.setFilter(new JwtFilter());
        //需要使用jwt进行校验的资源路径，可以配置多个路径
        registrationBean.addUrlPatterns(uri);
        return registrationBean;
    }

}
